#### Tables S2, S3, Figure S4: Number of Norm Violation Support ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(srvyr)
# library(gt)

# data_pnas = import(here("Data","data_pnas.rds"))
# source(here("Code","Functions","funcs_theme.R"))

norm_ind = paste0("norm_",c("judges","polling","executive","censorship","loyalty"),"re")
norm_ind_sub = paste0("norm_",c("judges","polling","censorship","loyalty"),"re")

# Calculate number of norm violations approved by respondents
norm_numbers = data_pnas |> 
  filter(pid %in% c("Democrat","Republican")) |> 
  select(pid, all_of(norm_ind), uid, weight) |> 
  rowwise() |> # Slow
  mutate(norm_sum = sum(c_across(all_of(norm_ind))),
         norm_sum_sub = sum(c_across(all_of(norm_ind_sub))))

#### Table S2 ####
tab_s2 = norm_numbers |> 
  ungroup() |> # Removes rowwise
  select(pid, uid, weight, norm_sum) |> 
  drop_na(norm_sum) |> 
  as_survey_design(ids = uid, weights = weight) |> 
  group_by(pid, norm_sum) |> 
  summarise(pct = survey_prop(vartype = "ci")) |> 
  ungroup() |> 
  mutate(across(starts_with("pct"), \(x) round(x, digits = 3)*100),
         tab_stat = paste0(pct,"%<br>[",pct_low,",",pct_upp,"]")) |> 
  select(pid, norm_sum, tab_stat) |> 
  pivot_wider(names_from = norm_sum, values_from = tab_stat) |> 
  gt() |> 
  fmt_markdown(columns = everything()) |> 
  tab_spanner(label = "Norm Violations Supported (95% confidence intervals)", columns = 2:7) |> 
  cols_label(
    .list = c(
      "pid" = "Party"
    )
  ) |> 
  cols_align(
    align = "center",
    columns = 2:7
  ) 

print(tab_s2)

tab_s2 |> 
  as_latex() |> as.character() |> 
  writeLines(con = here("Tables","Supplementary","table_s2.tex"))

#### Table S3 ####

tab_s3 = norm_numbers |> 
  ungroup() |> # Removes rowwise
  select(pid, uid, weight, norm_sum_sub) |> 
  drop_na(norm_sum_sub) |> 
  as_survey_design(ids = uid, weights = weight) |> 
  group_by(pid, norm_sum_sub) |> 
  summarise(pct = survey_prop(vartype = "ci")) |> 
  ungroup() |> 
  mutate(across(starts_with("pct"), \(x) round(x, digits = 3)*100),
         tab_stat = paste0(pct,"%<br>[",pct_low,",",pct_upp,"]")) |> 
  select(pid, norm_sum_sub, tab_stat) |> 
  pivot_wider(names_from = norm_sum_sub, values_from = tab_stat) |> 
  gt() |> 
  fmt_markdown(columns = everything()) |> 
  tab_spanner(label = "Norm Violations Supported (95% confidence intervals)", columns = 2:6) |> 
  cols_label(
    .list = c(
      "pid" = "Party"
    )
  ) |> 
  cols_align(
    align = "center",
    columns = 2:6
  ) 

print(tab_s3)

tab_s3 |> 
  as_latex() |> as.character() |> 
  writeLines(con = here("Tables","Supplementary","table_s3.tex"))

#### Figure S4 ####

fig_s4 = norm_numbers |> 
  ungroup() |> # Removes rowwise
  mutate(norm_exec = norm_sum - norm_sum_sub) |> 
  select(pid, uid, weight, norm_exec, norm_sum) |> 
  drop_na(norm_sum) |> 
  as_survey_design(ids = uid, weights = weight) |> 
  group_by(pid, interact(norm_exec, norm_sum)) |> 
  summarise(pct = survey_prop(vartype = "ci")) |> 
  ungroup() |> 
  group_by(pid, norm_sum) |> 
  mutate(
    pct = case_when(
      norm_exec == 1 ~ sum(pct),
      T ~ pct),
    is_max = pct >= sum(pct)/2
  ) %>%
  ggplot(aes(x = as.factor(norm_sum), y = pct, fill = pid)) +
  geom_col(data = . %>% filter(norm_exec == 0),
           position = position_dodge(0.9), alpha = 1) +
  geom_col(data = . %>% filter(norm_exec == 1),
           position = position_dodge(0.9), alpha = 0.3) +
  geom_tile(aes(y = NA_integer_, alpha = factor(norm_exec))) +
  geom_text(data = . %>% filter(is_max == 1),
            aes(x = as.factor(norm_sum), y = pct + .05,
                label = paste0(round(pct, 3)*100, "%")),
            color = "black", size = 3, position = position_dodge(0.9)) +
  scale_y_continuous(labels = scales::percent,
                     limits = c(0,1)) +
  scale_fill_manual(name = "Party",
                    values = c("blue","red")) +
  scale_alpha_manual(name = "Exec. Power",
                     labels = c("No","Yes"),
                     values = c(1,.3)) +
  labs(y = "Percent Supporting",
       x = "Number of Norm Violations Supported") +
  theme_prl() +
  theme(legend.justification = c(1, 1), legend.position = c(1, 1))

print(fig_s4)

ggsave(here("Plots","Supplementary","figure_s4.png"),
       units = "in", width = 6, height = 3, dpi = 600)